package indi.storyoftime.rasite.controller;


import indi.storyoftime.rasite.entity.Album;
import indi.storyoftime.rasite.entity.Image;
import indi.storyoftime.rasite.service.impl.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
@RequestMapping("/albums")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /**
     * 分页获取album。
     */
    @GetMapping(value = "", produces = "application/json;charset=UTF-8")
    public List<Album> getAlbums(
            @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "offset", required = false) Integer offset,
            @RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
            @RequestParam(value = "desc", required = false, defaultValue = "true") boolean desc) {
        List<Album> albums;

        if (pageNo != null && pageSize != null) {
            // 分页查询
            albums = albumService.getAlbumsByPage(pageNo, pageSize, orderBy, desc);
        } else if (limit != null) {
            // 使用limit查询
            if (offset == null) {
                offset = 0;
            }
            albums = albumService.getAlbumsByLimit(offset, limit, orderBy, desc);
        } else {
            albums = albumService.getAlbums();
        }

        return albums;
    }

    /**
     * 新增album。
     */
    @PostMapping(value = "", produces = "application/json;charset=UTF-8")
    public Album createAlbum(@RequestBody Album album) {
        if (albumService.createAlbum(album) == 1) {
            return album;
        } else {
            return null;
        }
    }

    /**
     * 根据id更新album。
     */
    @PutMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public void updateAlbumById(@PathVariable Integer id, @RequestBody Album album) {
        albumService.updateAlbumById(id, album);
    }

    /**
     * 根据id部分更新album。
     */
    @PatchMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public void updateAlbumPartlyById(@PathVariable Integer id, @RequestBody Album album) {
        albumService.updateAlbumById(id, album);
    }

    /**
     * 根据id获取album。
     */
    @GetMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public Album getAlbumById(@PathVariable Integer id) {
        return albumService.getAlbumById(id);
    }

    /**
     * 根据id删除album。
     */
    @DeleteMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public void deleteAlbumById(@PathVariable Integer id) {
        albumService.deleteAlbumById(id);
    }

    /**
     * 根据albumId分页获取image。
     */
    @GetMapping(value = "/{albumId}/images", produces = "application/json;charset=UTF-8")
    public List<Image> getImagesInAlbum(
            @PathVariable Integer albumId,
            @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "offset", required = false) Integer offset,
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @RequestParam(value = "desc", required = false) Boolean desc) {
        List<Image> images;

        if (Objects.nonNull(pageNo) && Objects.nonNull(pageSize)) {
            // 分页查询
            return albumService.getImagesInAlbumByPage(albumId, pageNo, pageSize, orderBy, desc);
        } else if (limit != null) {
            // 使用limit查询
            if (!Objects.nonNull(offset)) {
                offset = 0;
            }
            images = albumService.getImagesInAlbumByLimit(albumId, offset, limit);
        } else {
            images = albumService.getImagesInAlbum(albumId);
        }

        return images;
    }
}
